/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
 * MA  02110-1301, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */
package system.teleport;

import java.util.HashMap;
import java.util.Map;

import com.aionemu.gameserver.model.DescriptionId;
import com.aionemu.gameserver.model.Race;
import com.aionemu.gameserver.model.gameobjects.Item;
import com.aionemu.gameserver.model.gameobjects.VisibleObject;
import com.aionemu.gameserver.model.gameobjects.player.Player;
import com.aionemu.gameserver.model.gameobjects.player.RequestResponseHandler;
import com.aionemu.gameserver.network.aion.serverpackets.SM_QUESTION_WINDOW;
import com.aionemu.gameserver.questEngine.QuestEngine;
import com.aionemu.gameserver.questEngine.handlers.QuestHandler;
import com.aionemu.gameserver.questEngine.model.QuestEnv;
import com.aionemu.gameserver.services.TeleportService;
import com.aionemu.gameserver.utils.PacketSendUtility;

/**
 * @author Mr. Poke
 *
 */
public class _1002AbyssExit extends QuestHandler
{

	private final static int	questId	= -1002;

	private static Map<Integer, PortalData> portals = new HashMap<Integer, PortalData>();
	static
	{
		//Abyss to Verteron
		portals.put(730073, new PortalData(210030000, 1643, 1500, 119, 1600, 800983, Race.ELYOS));
		//Abyss to Eltnen
		portals.put(730074, new PortalData(210020000, 343, 2724, 264, 2800, 800985, Race.ELYOS));
		//Abyss to Heiron
		portals.put(730075, new PortalData(210040000, 2540, 343, 411, 5000, 800987, Race.ELYOS));

		//Abyss to Altgard
		portals.put(730076, new PortalData(220030000, 1748, 1807, 254, 1600, 800993, Race.ASMODIANS));
		//Abyss to Morheim
		portals.put(730077, new PortalData(220020000, 308, 2274, 449, 2800, 800995, Race.ASMODIANS));
		//Abyss to Beluslan
		portals.put(730078, new PortalData(220040000, 325, 336, 229, 5000, 800997, Race.ASMODIANS));
	}
	private static class PortalData
	{
		private int worldId;
		private int x;
		private int y;
		private int z;
		private int price;
		private int locNameId;
		private Race race;
		/**
		 * @param worldId
		 * @param x
		 * @param y
		 * @param z
		 * @param price
		 * @param locNameId
		 * @param race
		 */

		public PortalData(int worldId, int x, int y, int z, int price, int locNameId, Race race)
		{
			super();
			this.worldId = worldId;
			this.x = x;
			this.y = y;
			this.z = z;
			this.price = price;
			this.locNameId = locNameId;
			this.race = race;
		}
	}

	/**
	 * @param questId
	 */
	public _1002AbyssExit()
	{
		super(questId);
		for (int npcId: portals.keySet())
			qe.setNpcQuestData(npcId).addOnTalkEvent(questId);
	}

	@Override
	public boolean onDialogEvent(final QuestEnv env)
	{
		int npcId = env.getVisibleObject().getNpcId();
		final PortalData data = portals.get(npcId);
		if (data == null)
			return false;
		final Player player = env.getPlayer();
		if (player.getRace() != data.race)
			return false;
		new NpcUseAction(player, env.getVisibleObject())
		{
			@Override
			protected void end()
			{
				super.end();
				RequestResponseHandler responseHandler = new RequestResponseHandler(env.getVisibleObject())
				{
					@Override
					public void acceptRequest(VisibleObject requester, Player responder)
					{
						if (Item.decreaseKinah(player, data.price))
						{
							TeleportService.teleportTo(player, data.worldId, data.x, data.y, data.z, 0);
						}
						//TODO System message?
					}

					@Override
					public void denyRequest(VisibleObject requester, Player responder)
					{
					}
				};
				
				boolean requested = player.getResponseRequester().putRequest(SM_QUESTION_WINDOW.STR_TELEPORT_NEED_CONFIRM, responseHandler);
				if (requested)
				{
					PacketSendUtility.sendPacket(player, new SM_QUESTION_WINDOW(SM_QUESTION_WINDOW.STR_TELEPORT_NEED_CONFIRM, env.getVisibleObject().getObjectId(), new DescriptionId(data.locNameId), Integer.toString(data.price)));
				}
			}
		};
		return true;
	}

	public static void main(String[] args)
	{
		QuestEngine.getInstance().addQuestHandler(new _1002AbyssExit());
	}
}
